//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.basic.action;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.base.entity.BaseEntity;
import net.mingsoft.base.entity.ResultData;
import net.mingsoft.basic.bean.CityBean;
import net.mingsoft.basic.bean.EUListBean;
import net.mingsoft.basic.biz.ICityBiz;
import net.mingsoft.basic.entity.CityEntity;
import net.mingsoft.basic.util.BasicUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

@Api(
        tags = {"后端-基础接口"}
)
@Controller
@RequestMapping({"/${ms.manager.path}/basic/city"})
public class CityAction extends BaseAction {
    @Autowired
    private ICityBiz cityBiz;

    public CityAction() {
    }

    @ApiIgnore
    @ApiOperation("返回主界面index")
    @GetMapping({"/index"})
    @RequiresPermissions({"city:view"})
    public String index(HttpServletResponse response, HttpServletRequest request) {
        return "/WEB-INF/manager/basic/city/index";
    }

    @ApiOperation("返回编辑界面city_form")
    @ApiImplicitParam(
            name = "id",
            value = "主键编号",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/form"})
    @RequiresPermissions({"city:view"})
    @ApiIgnore
    public String form(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (!StringUtils.isEmpty(city.getId())) {
            BaseEntity cityEntity = this.cityBiz.getEntity(Integer.parseInt(city.getId()));
            model.addAttribute("cityEntity", cityEntity);
        }

        return "/WEB-INF/manager/basic/city/form";
    }

    @ApiOperation("查询省市县镇村数据列表")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "provinceId",
            value = "省／直辖市／自治区级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "provinceName",
            value = "省／直辖市／自治区级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityId",
            value = "市级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityName",
            value = "市级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityPy",
            value = "城市拼音首字母",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "countyId",
            value = "县／区级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "countyName",
            value = "县／区级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "townId",
            value = "街道／镇级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "townName",
            value = "街道／镇级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "villageId",
            value = "村委会id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "villageName",
            value = "村委会名称",
            required = false,
            paramType = "query"
    )})
    @RequestMapping(
            value = {"/list"},
            method = {RequestMethod.GET, RequestMethod.POST}
    )
    @RequiresPermissions({"city:view"})
    @ResponseBody
    public ResultData list(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        BasicUtil.startPage();
        new ArrayList();
        List cityList;
        if (city.getProvinceId() == null) {
            cityList = this.cityBiz.queryProvinceAndName(city);
        } else {
            cityList = this.cityBiz.queryById(city);
        }

        return ResultData.build().success(new EUListBean(cityList, (int)BasicUtil.endPage(cityList).getTotal()));
    }

    @ApiOperation("获取省市县镇村数据")
    @ApiImplicitParam(
            name = "id",
            value = "主键编号",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/get"})
    @RequiresPermissions({"city:view"})
    @ResponseBody
    public ResultData get(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request, @ApiIgnore ModelMap model) {
        if (StringUtils.isEmpty(city.getId())) {
            return ResultData.build().error(this.getResString("err.error", new String[]{this.getResString("id")}));
        } else {
            CityEntity _city = (CityEntity)this.cityBiz.getEntity(Integer.parseInt(city.getId()));
            return ResultData.build().success(_city);
        }
    }

    @ApiOperation("保存省市县镇村数据实体")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "provinceId",
            value = "省／直辖市／自治区级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "provinceName",
            value = "省／直辖市／自治区级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityId",
            value = "市级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityName",
            value = "市级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityPy",
            value = "城市拼音首字母",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "countyId",
            value = "县／区级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "countyName",
            value = "县／区级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "townId",
            value = "街道／镇级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "townName",
            value = "街道／镇级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "villageId",
            value = "村委会id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "villageName",
            value = "村委会名称",
            required = false,
            paramType = "query"
    )})
    @RequiresPermissions({"city:save"})
    @PostMapping({"/save"})
    @ResponseBody
    public ResultData save(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isBlank(city.getProvinceName()) && StringUtils.isBlank(city.getCityName()) && StringUtils.isBlank(city.getCountyName()) && StringUtils.isBlank(city.getTownName()) && StringUtils.isBlank(city.getVillageName())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("area")}));
        } else {
            Snowflake snowflake = IdUtil.getSnowflake(0L, 0L);
            CityEntity _city = new CityEntity();
            BeanUtils.copyProperties(city, _city);
            _city.setId((String)null);
            _city.setCreateDate((Date)null);
            _city.setUpdateDate((Date)null);
            _city.setCreateBy((String)null);
            LambdaQueryWrapper<CityEntity> wrapper = new LambdaQueryWrapper(_city);
            List<CityEntity> list = this.cityBiz.list(wrapper);
            if (list != null && list.size() > 0) {
                return ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("area")}));
            } else {
                CityEntity queryEntity = new CityEntity();
                queryEntity.setProvinceId(city.getProvinceId());
                queryEntity.setCityId(city.getCityId());
                queryEntity.setCountyId(city.getCountyId());
                queryEntity.setTownId(city.getTownId());
                queryEntity.setVillageId(city.getVillageId());
                wrapper = new LambdaQueryWrapper(queryEntity);
                List<CityEntity> citys = this.cityBiz.list(wrapper);
                CityEntity cityEntity = null;
                if (CollUtil.isNotEmpty(citys)) {
                    cityEntity = (CityEntity)this.cityBiz.list(wrapper).get(0);
                }

                if (city.getProvinceId() != null) {
                    city.setProvinceName(cityEntity.getProvinceName());
                    city.setCityId(snowflake.nextId());
                } else if (city.getCityId() != null) {
                    city.setProvinceId(cityEntity.getProvinceId());
                    city.setProvinceName(cityEntity.getProvinceName());
                    city.setCityName(cityEntity.getCityName());
                    city.setCountyId(snowflake.nextId());
                } else if (city.getCountyId() != null) {
                    city.setProvinceId(cityEntity.getProvinceId());
                    city.setProvinceName(cityEntity.getProvinceName());
                    city.setCityId(cityEntity.getCityId());
                    city.setCityName(cityEntity.getCityName());
                    city.setCountyId(cityEntity.getCountyId());
                    city.setCountyName(cityEntity.getCountyName());
                    city.setTownId(snowflake.nextId());
                } else if (city.getTownId() != null) {
                    city.setProvinceId(cityEntity.getProvinceId());
                    city.setProvinceName(cityEntity.getProvinceName());
                    city.setCityId(cityEntity.getCityId());
                    city.setCityName(cityEntity.getCityName());
                    city.setCountyId(cityEntity.getCountyId());
                    city.setCountyName(cityEntity.getCountyName());
                    city.setTownId(cityEntity.getTownId());
                    city.setTownName(cityEntity.getTownName());
                    city.setVillageId(snowflake.nextId());
                } else if (city.getProvinceId() == null) {
                    city.setProvinceId(snowflake.nextId());
                }

                this.cityBiz.save(city);
                this.cityBiz.updateCache();
                return ResultData.build().success(city);
            }
        }
    }

    @ApiOperation("批量删除省市县镇村数据")
    @RequiresPermissions({"city:del"})
    @PostMapping({"/delete"})
    @ResponseBody
    public ResultData delete(@RequestBody List<CityEntity> citys, HttpServletResponse response, HttpServletRequest request) {
        Iterator var4 = citys.iterator();

        while(var4.hasNext()) {
            CityEntity city = (CityEntity)var4.next();
            this.cityBiz.deleteEntity(city);
        }

        this.cityBiz.updateCache();
        return ResultData.build().success();
    }

    @ApiOperation("更新省市县镇村数据信息省市县镇村数据")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "id",
            value = "主键编号",
            required = true,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "provinceId",
            value = "省／直辖市／自治区级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "provinceName",
            value = "省／直辖市／自治区级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityId",
            value = "市级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityName",
            value = "市级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "cityPy",
            value = "城市拼音首字母",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "countyId",
            value = "县／区级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "countyName",
            value = "县／区级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "townId",
            value = "街道／镇级id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "townName",
            value = "街道／镇级名称",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "villageId",
            value = "村委会id",
            required = false,
            paramType = "query"
    ), @ApiImplicitParam(
            name = "villageName",
            value = "村委会名称",
            required = false,
            paramType = "query"
    )})
    @RequiresPermissions({"city:update"})
    @PostMapping({"/update"})
    @ResponseBody
    public ResultData update(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request) {
        if (StringUtils.isBlank(city.getProvinceName()) && StringUtils.isBlank(city.getCityName()) && StringUtils.isBlank(city.getCountyName()) && StringUtils.isBlank(city.getTownName()) && StringUtils.isBlank(city.getVillageName())) {
            return ResultData.build().error(this.getResString("err.empty", new String[]{this.getResString("area")}));
        } else {
            CityEntity _city = new CityEntity();
            _city.setProvinceName(city.getProvinceName());
            _city.setCityName(city.getCityName());
            _city.setCountyName(city.getCountyName());
            _city.setTownName(city.getTownName());
            _city.setVillageName(city.getVillageName());
            LambdaQueryWrapper<CityEntity> wrapper = new LambdaQueryWrapper(_city);
            List<CityEntity> list = this.cityBiz.list(wrapper);
            if (list != null && list.size() > 1) {
                return ResultData.build().error(this.getResString("err.exist", new String[]{this.getResString("area")}));
            } else {
                this.cityBiz.updateEntity(city);
                this.cityBiz.updateCache();
                return ResultData.build().success(city);
            }
        }
    }

    @ApiOperation("查询省列表")
    @GetMapping({"/province"})
    @ResponseBody
    public ResultData province(HttpServletResponse response, HttpServletRequest request) {
        List cityList = this.cityBiz.queryProvince();
        return ResultData.build().success(cityList);
    }

    @ApiOperation("根据省id查询城市列表")
    @ApiImplicitParam(
            name = "provinceId",
            value = "省／直辖市／自治区级id",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/city"})
    @ResponseBody
    public ResultData city(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request) {
        List cityList = this.cityBiz.queryCity(city);
        return ResultData.build().success(cityList);
    }

    @ApiOperation("根据城市id查询区域列表")
    @ApiImplicitParam(
            name = "cityId",
            value = "市级id",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/county"})
    @ResponseBody
    public ResultData county(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request) {
        List cityList = this.cityBiz.queryCounty(city);
        return ResultData.build().success(cityList);
    }

    @ApiOperation("根据区县id查询城镇列表")
    @ApiImplicitParam(
            name = "countyId",
            value = "区县Id",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/town"})
    @ResponseBody
    public ResultData town(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request) {
        BasicUtil.startPage();
        List cityList = this.cityBiz.queryTown(city);
        return ResultData.build().success(cityList);
    }

    @ApiOperation("根据城镇id查询街道列表")
    @ApiImplicitParam(
            name = "townId",
            value = "城镇Id",
            required = true,
            paramType = "query"
    )
    @GetMapping({"/village"})
    @ResponseBody
    public ResultData village(@ModelAttribute @ApiIgnore CityEntity city, HttpServletResponse response, HttpServletRequest request) {
        List cityList = this.cityBiz.queryVillage(city);
        return ResultData.build().success(cityList);
    }

    @ApiOperation("生成json文件")
    @PostMapping({"/createJson"})
    @ResponseBody
    @RequiresPermissions({"city:createJson"})
    public ResultData creatJson(HttpServletResponse response, HttpServletRequest request) {
        int level = 5;
        String type = "tree";
        List<CityBean> cityList = this.cityBiz.queryForTree(level, type);
        File file = FileUtil.file(BasicUtil.getRealPath((String)null), "/static/json/city.json");
        FileUtil.writeBytes(JSONUtil.toJsonStr(ResultData.build().success(cityList)).getBytes(StandardCharsets.UTF_8), file);
        return ResultData.build().success();
    }
}
